package Demo.demo10;

import java.util.Scanner;

public class Demo13 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double r = Math.random();
        int res = (int) (r * 9 + 1);
        int flag = 0;

        while (flag == 0) {
            int[] check = new int[10];
            for (int i = 0; i < 10; i++) check[i] = 0;
            check[res / 1000] += 1;
            check[(res / 100) % 10] += 1;
            check[(res / 10) % 10] += 1;
            check[res % 10] += 1;
            for (int i = 0; i < 10; i++)
                if (check[i] >= 2) {
                    r = Math.random();
                    res = (int) (r * 8999 + 1000);
                    flag = 0;
                    break;
                } else flag = 1;
        }
        System.out.println("答案: " + res);
        int input = -1;
        int idx = 0;
        int times = 0;
        while (input != res) {
            System.out.print("请输入你猜的数字： ");
            input = scanner.nextInt();
            int inputcopy = input;

            if (input < 0) {
                System.out.println("您输入的数字不是四位数！");
                times++;
                continue;
            }
            int t = 0;

        while (inputcopy != 0) {
            inputcopy /= 10;
            t++;
        }
        if (t != 4) {
            System.out.println("您输入的数字不是四位数！");
            times++;
            continue;
        }
            int n = 0, m = 0;// nAmB
            if (input == res) break;
// 输入的各个位数
            int[] a = new int[4];
            a[0] = input / 1000;
            a[1] = (input / 100) % 10;
            a[2] = (input / 10) % 10;
            a[3] = (input) % 10;

// 答案的各个位数

            int[] ans = new int[4];

            ans[0] = res / 1000;
            ans[1] = (res / 100) % 10;
            ans[2] = (res / 10) % 10;
            ans[3] = (res) % 10;

            for (int i = 0; i < 4; i++) {
                if (ans[i] == a[i]) n += 1; // A的数量
                for (int j = 0; j < 4; j++) {// B的数量
                    if (ans[j] == a[i] && j != i) m += 1;
                }
            }
            System.out.print((++idx) + ": " + n + "A" + m + "B");
            System.out.println();
            times++;
        }
        if (input == res) {
            times++;
            System.out.println("4A0B");
            System.out.println("你很厉害啊！");
            System.out.println("猜测次数: " + times);
        }
    }
    }
